package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.DepartmentService;
import cn.itsource.pethome.util.AjaxResult;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentContorller {

    @Autowired
    private DepartmentService departmentService;

    @PutMapping
    private AjaxResult save(@RequestBody Department department){
        try {

            departmentService.save(department);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"保存失败"+e.getMessage());
        }
    }

    @PostMapping
    private AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"添加失败"+e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    private AjaxResult delect(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            System.out.println("id="+ id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }
    //批量删除
    @PatchMapping("/batchRemove")
    private AjaxResult batchRemove(@RequestBody  List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            System.out.println("ids="+ ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败"+e.getMessage());
        }
    }

    @GetMapping("{id}")
    private Department loadById(@PathVariable("id") Long id){
        Department department = departmentService.loadById(id);
        return department;

    }
    @PatchMapping("/loadAll")
    private List<Department> loadAll(){
        return departmentService.loadAll();
    }

    //展示f分页列表
    @PatchMapping
    private PageList<Department> loadPageByQuery(@RequestBody DepartmentQuery query){

        return departmentService.loadPageByQuery(query);

    }
}
